<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="${rc.contextPath}/static/plugins/layui/css/layui.css" media="all"/>

    <script type="text/javascript" src="${rc.contextPath}/static/libs/jquery.min.js"></script>
    <script type="text/javascript" src="${rc.contextPath}/static/plugins/layui/layui.js"></script>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form(),
                layer = layui.layer,
                layedit = layui.layedit,
                laydate = layui.laydate;

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');
            //自定义验证规则
            form.verify({
                title: function (value) {
                    if (value.length < 5) {
                        return '标题至少得5个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                content: function (value) {
                    layedit.sync(editIndex);
                }
            });

            //监听提交
            form.on('submit(demo1)', function (data) {
                layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                })
                return false;
            });
        });

        //添加屬性
        function addParam(obj) {
            $(obj).hide();
            $(obj).next().show();
            var html = '<div class="layui-form-item"> ' +
                '<div class="layui-inline"> ' +
                '<label class="layui-form-label" style="width: 84px"></label> ' +
                '<div class="layui-input-inline"> ' +
                '<input name="head_name" placeholder="列名" type="text" class="layui-input"> </div> ' +
                '<div class="layui-input-inline"> <input type="text" name="column_name" placeholder="對應字段" class="layui-input"> </div> ' +
                '<div class="layui-btn-group"> ' +
                '<button class="layui-btn " onclick="addParam(this)"> <i class="layui-icon">&#xe654;</i> </button> ' +
                '<button  onclick="removeThis(this)" class="layui-btn "> <i class="layui-icon">&#xe640;</i> </button> </div> </div> </div>';
            $("#params").append(html);
        }


        //刪除屬性
        function removeThis(obj) {
            $(obj).parent().parent().remove();
        }

        //驗證屬性
        function verification() {
            var basepath = "${rc.contextPath}";
            var head_name = "";
            var column_name = "";
            var head_names = $("input[name='head_name']");
            var column_names = $("input[name='column_name']");
            for (var i = 0;i<head_names.length;i++){
                head_name += $(head_names[i]).val()+",";
                column_name += $(column_names[i]).val()+",";
            }

            var datas = {
                "reportName":$("#name").val(),
                "sqlStr":$("#sql").val(),
                "columnName":column_name,
                "headName":head_name
            };
            $.ajax({
                type: "POST",
                contentType : "application/json ; charset=utf-8",
                url: basepath+"/sys/report/save",
                data: JSON.stringify(datas),
                dataType: "json",
                success: function(msg){
                    console.log(msg);
                }
            });
        }
    </script>
</head>

<body>
<div style="margin: 15px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加報表</legend>
    </fieldset>


    <div class="layui-form-item">
        <label class="layui-form-label">单行输入框</label>
        <div class="layui-input-block">
            <input id="name" type="text" name="title" autocomplete="off" placeholder="请输入報表名" class="layui-input"
                   style="width: 450px">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">sql語句</label>
        <div class="layui-input-block">
            <textarea id="sql" placeholder="请输入sql語句" class="layui-textarea"></textarea>
        </div>
    </div>
    <div id="params">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 84px">字段對應關係</label>
                <div class="layui-input-inline">
                    <input name="head_name" type="text" placeholder="列名" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <input name="column_name" type="text" placeholder="對應字段" class="layui-input">
                </div>
                <div class="layui-btn-group">
                    <button class="layui-btn " onclick="addParam(this)">
                        <i class="layui-icon">&#xe654;</i>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1" onclick="verification()">立即提交</button>
        </div>
    </div>
</div>
</body>

</html>